<template>
  <div class="xtx-pagination" v-if="totalComment>0">
    <a
      href="javascript:;"
      :class="{ disabled: currentPage == 1 }"
      @click="changePage(currentPage - 1)"
      >上一页</a
    >
    <span v-show="pager.startBtn != 1">...</span>
    <a
      href="javascript:;"
      :class="{ active: item === currentPage }"
      @click="changePage(item)"
      v-for="item in pager.btnArr"
      >{{ item }}</a
    >
    <span v-show="pager.endBtn != pager.totalPage">...</span>
    <a
      href="javascript:;"
      :class="{ disabled: currentPage == pager.totalPage }"
      @click="changePage(currentPage + 1, pager.totalPage)"
      >下一页</a
    >
  </div>
</template>
<script>
import { ref, computed, watch } from 'vue'
export default {
  name: 'PageInation',
  props: {
    totalComment: {
      type: Number,
      default: 0
    },
    pageSize: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  setup (props, { emit }) {
    // 按钮个数
    const count = 5
    // 当前页码
    const currentPage = ref(1)
    // 总条数
    const totalComment = ref(0)
    // 每页条数
    const pageSize = ref(10)
    const pager = computed(() => {
      const totalPage = Math.ceil(totalComment.value / pageSize.value)
      // 理想情况下，偏移按钮
      const offset = Math.floor(count / 2)
      let startBtn = currentPage.value - offset
      let endBtn = startBtn + count - 1
      // 特殊情况
      // 1、起始页码小于1
      if (startBtn < 1) {
        startBtn = 1
        // 再处理结束情况，如果大于总页数
        endBtn =
          startBtn + count - 1 > totalPage ? totalPage : startBtn + count - 1
      }
      // 2、结束页码大于总页数
      if (endBtn > totalPage) {
        endBtn = totalPage
        startBtn = endBtn - count + 1 < 1 ? 1 : endBtn - count + 1
      }
      const btnArr = []
      for (let i = startBtn; i <= endBtn; i++) {
        btnArr.push(i)
      }
      // console.log(btnArr, startBtn, endBtn, totalPage);
      return { totalPage, btnArr, startBtn, endBtn }
    })

    // 这里是我自己定义的方法，对比老师的来说，这过于啰嗦，老师直接click修改currentPage就调用了计算属性进行重新运算，还是对计算属性不了解
    const changePage = (step, total) => {
      if (step < 1 || step > total) {

      } else {
        currentPage.value = step
        emit('currentChange', step)
      }
    }

    // 监听props变化，更新数据
    watch(
      props,
      () => {
        currentPage.value = props.currentPage
        pageSize.value = props.pageSize
        totalComment.value = props.totalComment
      },
      { immediate: true }
    )
    return { currentPage, pager, changePage }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>
